/*$Id: AbstractIDEPluginMojo.java 334 2011-05-14 14:35:12Z vip.delete $*/

/**
 * Copyright vip_delete
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.googlecode.ideaplugin;

import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.factory.ArtifactFactory;
import org.apache.maven.artifact.manager.WagonManager;
import org.apache.maven.artifact.metadata.ArtifactMetadataSource;
import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.artifact.resolver.ArtifactResolver;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.project.MavenProject;
import org.apache.maven.project.MavenProjectBuilder;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Abstract Mojo for hiding maven and velocity logic
 *
 * @author vip_delete
 * @since 26.07.2010
 */
abstract class AbstractIDEPluginMojo extends AbstractMojo
{
    private static boolean initialized;

    private static final Object SYNC = new Object();

    /**
     * @parameter expression="${project}"
     * @required
     * @readonly
     */
    private MavenProject project;

    /**
     * @parameter expression="${reactorProjects}"
     * @required
     * @readonly
     */
    private List<MavenProject> reactorProjects;

    /**
     * @component
     */
    private WagonManager wagonManager;

    /**
     * @component
     */
    private ArtifactResolver artifactResolver;

    /**
     * @component
     */
    private ArtifactFactory artifactFactory;

    /**
     * @component
     */
    private MavenProjectBuilder projectBuilder;

    /**
     * @parameter expression="${localRepository}"
     * @required
     * @readonly
     */
    private ArtifactRepository localRepository;

    /**
     * @component role="org.apache.maven.artifact.metadata.ArtifactMetadataSource" hint="maven"
     */
    private ArtifactMetadataSource artifactMetadataSource;

    private static ArtifactHolder artifactHolder;

    private static VelocityWorker velocityWorker;

    // Getters

    public List<MavenProject> getReactorProjects()
    {
        return reactorProjects;
    }

    protected MavenProject getProject()
    {
        return project;
    }

    protected ArtifactHolder getArtifactHolder()
    {
        return artifactHolder;
    }

    protected VelocityWorker getVelocityWorker()
    {
        return velocityWorker;
    }

    @Override
    public final void execute() throws MojoExecutionException, MojoFailureException
    {
        try
        {
            if (initialized)
            {
                doExecute();
            } else
            {
                synchronized (SYNC)
                {
                    if (!initialized)
                    {
                        if (artifactHolder == null)
                            artifactHolder = new ArtifactHolder(getLog(), wagonManager, reactorProjects, artifactFactory, artifactResolver, localRepository, artifactMetadataSource);
                        if (velocityWorker == null)
                            velocityWorker = new VelocityWorker();
                        initialized = true;
                    }
                }
                doExecute();
            }
        } catch (MojoExecutionException e)
        {
            throw e;
        } catch (MojoFailureException e)
        {
            throw e;
        } catch (Exception e)
        {
            throw new MojoFailureException(e.getMessage(), e);
        }
    }

    protected abstract void doExecute() throws Exception;

    // Velocity Bindings

    public List<Artifact> getDependencies(MavenProject project)
    {
        return artifactHolder.getDependencies(project);
    }

    public List<Artifact> getCommonDependencies()
    {
        return artifactHolder.getCommonDependencies();
    }

    public List<Artifact> getAllDependencies()
    {
        return artifactHolder.getAllDependencies();
    }

    public boolean isReactorArtifact(Artifact artifact)
    {
        return artifactHolder.isReactorArtifact(artifact);
    }

    public String getScope(Artifact artifact)
    {
        if (Artifact.SCOPE_PROVIDED.equalsIgnoreCase(artifact.getScope())) return "PROVIDED";
        if (Artifact.SCOPE_TEST.equalsIgnoreCase(artifact.getScope())) return "TEST";
        if (Artifact.SCOPE_RUNTIME.equalsIgnoreCase(artifact.getScope())) return "RUNTIME";
        return "COMPILE";
    }

    public boolean isWebFriendlyScope(Artifact artifact)
    {
        return !Artifact.SCOPE_PROVIDED.equalsIgnoreCase(artifact.getScope()) && !Artifact.SCOPE_TEST.equalsIgnoreCase(artifact.getScope());
    }

    public Map<String, String> getVcsMapping()
    {
        return artifactHolder.getVcsMapping();
    }

    public String getModuleLibraryJar(Artifact artifact)
    {
        return localRepository.pathOf(artifact);
    }

    public String getModuleLibraryJavadocs(Artifact artifact)
    {
        String path = localRepository.pathOf(artifact);
        return path.substring(0, path.length() - 4) + "-javadoc.jar";
    }

    public String getModuleLibrarySources(Artifact artifact)
    {
        String path = localRepository.pathOf(artifact);
        return path.substring(0, path.length() - 4) + "-sources.jar";
    }

    public String getLocalRepositoryBasePath()
    {
        return localRepository.getBasedir();
    }

    public String getReactorArtifactJarName(Artifact artifact)
    {
        return artifact.getArtifactId() + "-" + artifact.getVersion() + ".jar";
    }

    public List<String> getReactorPaths()
    {
        List<String> list = new ArrayList<String>();
        list.add(new File(project.getFile().getParentFile(), project.getArtifactId() + ".iml").getAbsolutePath());
        for (MavenProject reactor : (List<MavenProject>) project.getCollectedProjects())
            list.add(new File(reactor.getFile().getParentFile(), reactor.getArtifactId() + ".iml").getAbsolutePath());
        return list;
    }
}
